<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.utils.decorators &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django" href="../../django.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-utils-decorators">
            
  <h1>Source code for django.utils.decorators</h1><div class="highlight"><pre>
<span class="s">&quot;Functions that help with dynamically creating decorators for views.&quot;</span>

<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">wraps</span><span class="p">,</span> <span class="n">update_wrapper</span><span class="p">,</span> <span class="n">WRAPPER_ASSIGNMENTS</span>

<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>


<span class="k">class</span> <span class="nc">classonlymethod</span><span class="p">(</span><span class="nb">classmethod</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">owner</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">instance</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="s">&quot;This method is available only on the view class.&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">classonlymethod</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__get__</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">owner</span><span class="p">)</span>


<div class="viewcode-block" id="method_decorator"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.decorators.method_decorator">[docs]</a><span class="k">def</span> <span class="nf">method_decorator</span><span class="p">(</span><span class="n">decorator</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Converts a function decorator into a method decorator</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c"># &#39;func&#39; is a function at the time it is passed to _dec, but will eventually</span>
    <span class="c"># be a method of the class it is defined on.</span>
    <span class="k">def</span> <span class="nf">_dec</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">_wrapper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="nd">@decorator</span>
            <span class="k">def</span> <span class="nf">bound_func</span><span class="p">(</span><span class="o">*</span><span class="n">args2</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs2</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">func</span><span class="o">.</span><span class="n">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">))(</span><span class="o">*</span><span class="n">args2</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs2</span><span class="p">)</span>
            <span class="c"># bound_func has the signature that &#39;decorator&#39; expects i.e.  no</span>
            <span class="c"># &#39;self&#39; argument, but it is a closure over self so it can call</span>
            <span class="c"># &#39;func&#39; correctly.</span>
            <span class="k">return</span> <span class="n">bound_func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="c"># In case &#39;decorator&#39; adds attributes to the function it decorates, we</span>
        <span class="c"># want to copy those. We don&#39;t have access to bound_func in this scope,</span>
        <span class="c"># but we can cheat by using it on a dummy function.</span>

        <span class="nd">@decorator</span>
        <span class="k">def</span> <span class="nf">dummy</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">pass</span>
        <span class="n">update_wrapper</span><span class="p">(</span><span class="n">_wrapper</span><span class="p">,</span> <span class="n">dummy</span><span class="p">)</span>
        <span class="c"># Need to preserve any existing attributes of &#39;func&#39;, including the name.</span>
        <span class="n">update_wrapper</span><span class="p">(</span><span class="n">_wrapper</span><span class="p">,</span> <span class="n">func</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">_wrapper</span>

    <span class="n">update_wrapper</span><span class="p">(</span><span class="n">_dec</span><span class="p">,</span> <span class="n">decorator</span><span class="p">,</span> <span class="n">assigned</span><span class="o">=</span><span class="n">available_attrs</span><span class="p">(</span><span class="n">decorator</span><span class="p">))</span>
    <span class="c"># Change the name to aid debugging.</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">decorator</span><span class="p">,</span> <span class="s">&#39;__name__&#39;</span><span class="p">):</span>
        <span class="n">_dec</span><span class="o">.</span><span class="n">__name__</span> <span class="o">=</span> <span class="s">&#39;method_decorator(</span><span class="si">%s</span><span class="s">)&#39;</span> <span class="o">%</span> <span class="n">decorator</span><span class="o">.</span><span class="n">__name__</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">_dec</span><span class="o">.</span><span class="n">__name__</span> <span class="o">=</span> <span class="s">&#39;method_decorator(</span><span class="si">%s</span><span class="s">)&#39;</span> <span class="o">%</span> <span class="n">decorator</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span>
    <span class="k">return</span> <span class="n">_dec</span>

</div>
<div class="viewcode-block" id="decorator_from_middleware_with_args"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.decorators.decorator_from_middleware_with_args">[docs]</a><span class="k">def</span> <span class="nf">decorator_from_middleware_with_args</span><span class="p">(</span><span class="n">middleware_class</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Like decorator_from_middleware, but returns a function</span>
<span class="sd">    that accepts the arguments to be passed to the middleware_class.</span>
<span class="sd">    Use like::</span>

<span class="sd">         cache_page = decorator_from_middleware_with_args(CacheMiddleware)</span>
<span class="sd">         # ...</span>

<span class="sd">         @cache_page(3600)</span>
<span class="sd">         def my_view(request):</span>
<span class="sd">             # ...</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">make_middleware_decorator</span><span class="p">(</span><span class="n">middleware_class</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="decorator_from_middleware"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.decorators.decorator_from_middleware">[docs]</a><span class="k">def</span> <span class="nf">decorator_from_middleware</span><span class="p">(</span><span class="n">middleware_class</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Given a middleware class (not an instance), returns a view decorator. This</span>
<span class="sd">    lets you use middleware functionality on a per-view basis. The middleware</span>
<span class="sd">    is created with no params passed.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">make_middleware_decorator</span><span class="p">(</span><span class="n">middleware_class</span><span class="p">)()</span>

</div>
<span class="k">def</span> <span class="nf">available_attrs</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return the list of functools-wrappable attributes on a callable.</span>
<span class="sd">    This is required as a workaround for http://bugs.python.org/issue3445</span>
<span class="sd">    under Python 2.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY3</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">WRAPPER_ASSIGNMENTS</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">a</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">WRAPPER_ASSIGNMENTS</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="n">a</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">make_middleware_decorator</span><span class="p">(</span><span class="n">middleware_class</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">_make_decorator</span><span class="p">(</span><span class="o">*</span><span class="n">m_args</span><span class="p">,</span> <span class="o">**</span><span class="n">m_kwargs</span><span class="p">):</span>
        <span class="n">middleware</span> <span class="o">=</span> <span class="n">middleware_class</span><span class="p">(</span><span class="o">*</span><span class="n">m_args</span><span class="p">,</span> <span class="o">**</span><span class="n">m_kwargs</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">_decorator</span><span class="p">(</span><span class="n">view_func</span><span class="p">):</span>
            <span class="nd">@wraps</span><span class="p">(</span><span class="n">view_func</span><span class="p">,</span> <span class="n">assigned</span><span class="o">=</span><span class="n">available_attrs</span><span class="p">(</span><span class="n">view_func</span><span class="p">))</span>
            <span class="k">def</span> <span class="nf">_wrapped_view</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">middleware</span><span class="p">,</span> <span class="s">&#39;process_request&#39;</span><span class="p">):</span>
                    <span class="n">result</span> <span class="o">=</span> <span class="n">middleware</span><span class="o">.</span><span class="n">process_request</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">result</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                        <span class="k">return</span> <span class="n">result</span>
                <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">middleware</span><span class="p">,</span> <span class="s">&#39;process_view&#39;</span><span class="p">):</span>
                    <span class="n">result</span> <span class="o">=</span> <span class="n">middleware</span><span class="o">.</span><span class="n">process_view</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">view_func</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">result</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                        <span class="k">return</span> <span class="n">result</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">response</span> <span class="o">=</span> <span class="n">view_func</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">middleware</span><span class="p">,</span> <span class="s">&#39;process_exception&#39;</span><span class="p">):</span>
                        <span class="n">result</span> <span class="o">=</span> <span class="n">middleware</span><span class="o">.</span><span class="n">process_exception</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">result</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                            <span class="k">return</span> <span class="n">result</span>
                    <span class="k">raise</span>
                <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">response</span><span class="p">,</span> <span class="s">&#39;render&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">callable</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">render</span><span class="p">):</span>
                    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">middleware</span><span class="p">,</span> <span class="s">&#39;process_template_response&#39;</span><span class="p">):</span>
                        <span class="n">response</span> <span class="o">=</span> <span class="n">middleware</span><span class="o">.</span><span class="n">process_template_response</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">)</span>
                    <span class="c"># Defer running of process_response until after the template</span>
                    <span class="c"># has been rendered:</span>
                    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">middleware</span><span class="p">,</span> <span class="s">&#39;process_response&#39;</span><span class="p">):</span>
                        <span class="n">callback</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">response</span><span class="p">:</span> <span class="n">middleware</span><span class="o">.</span><span class="n">process_response</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">)</span>
                        <span class="n">response</span><span class="o">.</span><span class="n">add_post_render_callback</span><span class="p">(</span><span class="n">callback</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">middleware</span><span class="p">,</span> <span class="s">&#39;process_response&#39;</span><span class="p">):</span>
                        <span class="k">return</span> <span class="n">middleware</span><span class="o">.</span><span class="n">process_response</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">)</span>
                <span class="k">return</span> <span class="n">response</span>
            <span class="k">return</span> <span class="n">_wrapped_view</span>
        <span class="k">return</span> <span class="n">_decorator</span>
    <span class="k">return</span> <span class="n">_make_decorator</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
        <ul><li>django.utils.decorators</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>